//
//  VideoTitleView.swift
//  LearnDemo
//
//  Created by 振轩 on 2018/4/3.
//  Copyright © 2018年 振轩. All rights reserved.
//

import UIKit

class VideoTitleView: UIView {
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    var mode:ArticleDetail?{
        
        didSet{
            
            titleLabel.text = mode?.title
            if mode?.isLike == 0 {
                likeBTN.setImage(UIImage.init(named: "dz"), for: .normal)
            }else{
                likeBTN.setImage(UIImage.init(named: "dz2"), for: .normal)
            }
           
            collectCountLabel.text = "\(String(describing: mode!.collectNum))"
            likeCountLabel.text = "\(String(describing: mode!.likeNum))"
            if mode?.isCollect == 0 {
                collectBTN.setImage(UIImage.init(named: "sc2"), for: .normal)
            }else{
               collectBTN.setImage(UIImage.init(named: "sc"), for: .normal)
                
            }
            //likeCountLabel.text = "\(mode!.isLike)";
            ViewCountLabel.text = "\(mode!.viewNum)"+"次播放"
           
        }
    }
    @IBOutlet weak var titleLabel: UILabel!
    
    
    @IBOutlet weak var collectBTN: UIButton!
    
    @IBOutlet weak var likeBTN: UIButton!
    
    
    @IBAction func collectTap(_ sender: Any) {
        
       
        
        loadRefresh(url: "/index.php/FoodCheckApi/courseCollect.json",isLike: false)
        
        
        
        
    }
   //点赞./index.php/FoodCheckApi/courseLike.json
    @IBAction func LickClick(_ sender: Any) {
        
        
        
        
        loadRefresh(url: "/index.php/FoodCheckApi/courseLike.json", isLike: true)
        
        
    }
    
    
    @IBOutlet weak var collectCountLabel: UILabel!
    
    @IBOutlet weak var likeCountLabel: UILabel!
    
    
    
    
    @IBOutlet weak var ViewCountLabel: UILabel!
}



extension VideoTitleView {
    
    
    
    class func loadTitleView()->VideoTitleView{
        
        
        return Bundle.main.loadNibNamed("VideoTitleView", owner: nil, options: nil)?.first as! VideoTitleView
    }
    
    func loadRefresh(url:String,isLike:Bool){
        
        
        var params = [String:AnyObject]()
        params["courseId"] = mode?.id as AnyObject
        
        params["staffId"] = LoginStatuesModel.getStaffId() as AnyObject
        params["staffType"] = LoginStatuesModel.getUserType() as AnyObject
        let API:BaseReqApi = BaseReqApi.init(requestUrl: url, andrequestTime: 5, andParams: params as! [AnyHashable : Any], andRequestMethod: YTKRequestMethod.POST, andCache: false, andCacheTime: 0, andPostToken: true)
        API.starRequest { (status, mess, response) in
            
            guard let dic:Dictionary = response as? [String : AnyObject] else{
                
                return
            }
            print("_______***______")
            print(dic)
            print("_______***______")
            let Dir:[String:NSObject] = dic["data"] as! [String : NSObject]
            //let mode1  = CourseDetail.init(DIC: dic)
            if isLike{
               
                
                
                let mode1 :ArticleDetail = self.mode!
               
                mode1.isLike = Dir["isLike"] as! NSInteger
                mode1.likeNum = Dir["total"] as! NSInteger
                self.mode = mode1
                
               
            }else{
                
                let mode1 :ArticleDetail = self.mode!
              
                mode1.isCollect = Dir["isCollect"] as! NSInteger
                mode1.collectNum = Dir["total"] as! NSInteger
                
                self.mode = mode1
                
            }
            
            }
    }
}

//class LikeMode: NSObject {
//    var code:NSInteger = 0
//    var
//}
